home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / BNU22DC.ZIP / docs / binutils / ld.i1 (.txt) < prev    next >
GNU Info File  |  1993-09-06  |  49KB  |  1,006 lines

  1. This is Info file ld.info, produced by Makeinfo-1.52 from the input
  2. file ./ld.texinfo.
  3. START-INFO-DIR-ENTRY
  4. * Ld: (ld).            The GNU linker.
  5. END-INFO-DIR-ENTRY
  6.    This file documents the GNU linker LD.
  7.    Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.    Permission is granted to copy and distribute modified versions of
  12. this manual under the conditions for verbatim copying, provided also
  13. that the entire resulting derived work is distributed under the terms
  14. of a permission notice identical to this one.
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions.
  18. File: ld.info,  Node: Top,  Next: Overview,  Prev: (DIR),  Up: (DIR)
  19. Using ld
  20. ********
  21.    This file documents the GNU linker ld.
  22. * Menu:
  23. * Overview::                    Overview
  24. * Invocation::                  Invocation
  25. * Commands::                    Command Language
  26. * BFD::                         BFD
  27. * MRI::                         MRI Compatible Script Files
  28. * Index::                       Index
  29. File: ld.info,  Node: Overview,  Next: Invocation,  Prev: Top,  Up: Top
  30. Overview
  31. ********
  32.    `ld' combines a number of object and archive files, relocates their
  33. data and ties up symbol references. Usually the last step in compiling
  34. a program is to run `ld'.
  35.    `ld' accepts Linker Command Language files written in a superset of
  36. AT&T's Link Editor Command Language syntax, to provide explicit and
  37. total control over the linking process.
  38.    This version of `ld' uses the general purpose BFD libraries to
  39. operate on object files. This allows `ld' to read, combine, and write
  40. object files in many different formats--for example, COFF or `a.out'.
  41. Different formats may be linked together to produce any available kind
  42. of object file.  *Note BFD:: for a list of formats supported on various
  43. architectures.
  44.    Aside from its flexibility, the GNU linker is more helpful than other
  45. linkers in providing diagnostic information.  Many linkers abandon
  46. execution immediately upon encountering an error; whenever possible,
  47. `ld' continues executing, allowing you to identify other errors (or, in
  48. some cases, to get an output file in spite of the error).
  49. File: ld.info,  Node: Invocation,  Next: Commands,  Prev: Overview,  Up: Top
  50. Invocation
  51. **********
  52.    The GNU linker `ld' is meant to cover a broad range of situations,
  53. and to be as compatible as possible with other linkers.  As a result,
  54. you have many choices to control its behavior.
  55.    Here is a summary of the options you can use on the `ld' command
  56. line:
  57.      ld [-o OUTPUT ] OBJFILE...
  58.        [ -AARCHITECTURE ]  [ -b INPUT-FORMAT ]  [ -Bstatic ]
  59.        [ -c MRI-COMMANDFILE ]  [ -d | -dc | -dp ]
  60.        [ -defsym SYMBOL=EXPRESSION ]
  61.        [ -e ENTRY ]  [ -F ]  [ -F FORMAT ]
  62.        [ -format INPUT-FORMAT ]  [ -g ]  [ -i ]
  63.        [ -lAR ]  [ -LSEARCHDIR ]  [ -M | -m ]
  64.        [ -n | -N ]  [ -noinhibit-exec ]  [ -R FILENAME ]
  65.        [ -relax ]  [ -r | -Ur ]  [ -S ]  [ -s ]  [ -T COMMANDFILE ]
  66.        [ -Ttext TEXTORG ]  [ -Tdata DATAORG ]  [ -Tbss BSSORG ]
  67.        [ -t ]  [ -u SYM]  [-v]  [ -X ]  [ -x ]  [ -ySYMBOL ]
  68.        [ { SCRIPT } ]
  69.    This plethora of command-line options may seem intimidating, but in
  70. actual practice few of them are used in any particular context.  For
  71. instance, a frequent use of `ld' is to link standard Unix object files
  72. on a standard, supported Unix system.  On such a system, to link a file
  73. `hello.o':
  74.      ld -o OUTPUT /lib/crt0.o hello.o -lc
  75.    This tells `ld' to produce a file called OUTPUT as the result of
  76. linking the file `/lib/crt0.o' with `hello.o' and the library `libc.a',
  77. which will come from the standard search directories.  (See the
  78. discussion of the `-l' option below.)
  79.    The command-line options to `ld' may be specified in any order, and
  80. may be repeated at will.  Repeating most options with a different
  81. argument will either have no further effect, or override prior
  82. occurrences (those further to the left on the command line) of that
  83. option.
  84.    The exceptions--which may meaningfully be used more than once--are
  85. `-A', `-b' (or its synonym `-format'), `-defsym', `-L', `-l', `-R', and
  86. `-u'.
  87.    The list of object files to be linked together, shown as OBJFILE...,
  88. may follow, precede, or be mixed in with command-line options, except
  89. that an OBJFILE argument may not be placed between an option and its
  90. argument.
  91.    Usually the linker is invoked with at least one object file, but
  92. other forms of binary input files can also be specified with `-l',
  93. `-R', and the script command language.  If *no* binary input files at
  94. all are specified, the linker does not produce any output, and issues
  95. the message `No input files'.
  96.    Option arguments must either follow the option letter without
  97. intervening whitespace, or be given as separate arguments immediately
  98. following the option that requires them.
  99. `OBJFILE...'
  100.      The object files to be linked.
  101. `-b INPUT-FORMAT'
  102.      Specify the binary format for input object files that follow this
  103.      option on the command line.  You don't usually need to specify
  104.      this, as `ld' is configured to expect as a default input format
  105.      the most usual format on each machine.  INPUT-FORMAT is a text
  106.      string, the name of a particular format supported by the BFD
  107.      libraries.  `-format INPUT-FORMAT' has the same effect.  *Note
  108.      BFD::.
  109.      You may want to use this option if you are linking files with an
  110.      unusual binary format.  You can also use `-b' to switch formats
  111.      explicitly (when linking object files of different formats), by
  112.      including `-b INPUT-FORMAT' before each group of object files in a
  113.      particular format.
  114.      The default format is taken from the environment variable
  115.      `GNUTARGET'.  You can also define the input format from a script,
  116.      using the command `TARGET'; see *Note Other Commands::.
  117. `-Bstatic'
  118.      Ignored.  This option is accepted for command-line compatibility
  119.      with the SunOS linker.
  120. `-c MRI-COMMANDFILE'
  121.      For compatibility with linkers produced by MRI, `ld' accepts script
  122.      files written in an alternate, restricted command language,
  123.      described in *Note MRI Compatible Script Files: MRI.  Introduce
  124.      MRI script files with the option `-c'; use the `-T' option to run
  125.      linker scripts written in the general-purpose `ld' scripting
  126.      language.
  127. `-dc'
  128. `-dp'
  129.      These three options are equivalent; multiple forms are supported
  130.      for compatibility with other linkers.  They assign space to common
  131.      symbols even if a relocatable output file is specified (with
  132.      `-r').  The script command `FORCE_COMMON_ALLOCATION' has the same
  133.      effect.  *Note Other Commands::.
  134. `-defsym SYMBOL=EXPRESSION'
  135.      Create a global symbol in the output file, containing the absolute
  136.      address given by EXPRESSION.  You may use this option as many
  137.      times as necessary to define multiple symbols in the command line.
  138.      A limited form of arithmetic is supported for the EXPRESSION in
  139.      this context: you may give a hexadecimal constant or the name of
  140.      an existing symbol, or use `+' and `-' to add or subtract
  141.      hexadecimal constants or symbols.  If you need more elaborate
  142.      expressions, consider using the linker command language from a
  143.      script (*note Assignment: Symbol Definitions: Assignment.).
  144.      *Note:*  there should be no white space between SYMBOL, the equals
  145.      sign ("="), and EXPRESSION.
  146. `-e ENTRY'
  147.      Use ENTRY as the explicit symbol for beginning execution of your
  148.      program, rather than the default entry point. *Note Entry Point::,
  149.      for a discussion of defaults and other ways of specifying the
  150.      entry point.
  151. `-FFORMAT'
  152.      Ignored.  Some older linkers used this option throughout a
  153.      compilation toolchain for specifying object-file format for both
  154.      input and output object files.  The mechanisms `ld' uses for this
  155.      purpose (the `-b' or `-format' options for input files, the
  156.      `TARGET' command in linker scripts for output files, the
  157.      `GNUTARGET' environment variable) are more flexible, but `ld'
  158.      accepts the `-F' option for compatibility with scripts written to
  159.      call the old linker.
  160. `-format INPUT-FORMAT'
  161.      Synonym for `-b INPUT-FORMAT'.
  162.      Ignored.  Provided for compatibility with other tools.
  163.      Perform an incremental link (same as option `-r').
  164. `-lAR'
  165.      Add archive file AR to the list of files to link.  This option may
  166.      be used any number of times.  `ld' will search its path-list for
  167.      occurrences of `libAR.a' for every AR specified.
  168. `-LSEARCHDIR'
  169.      Add path SEARCHDIR to the list of paths that `ld' will search for
  170.      archive libraries.  You may use this option any number of times.
  171.      The paths can also be specified in a link script with the
  172.      `SEARCH_DIR' command.
  173.      Print (to the standard output) a link map--diagnostic information
  174.      about where symbols are mapped by `ld', and information on global
  175.      common storage allocation.
  176.      Set the text and data sections to be readable and writable.  Also,
  177.      do not page-align the data segment.  If the output format supports
  178.      Unix style magic numbers, mark the output as `OMAGIC'.
  179.      Set the text segment to be read only, and mark the output as
  180.      `NMAGIC' if possible.
  181. `-noinhibit-exec'
  182.      Retain the executable output file whenever it is still usable.
  183.      Normally, the linker will not produce an output file if it
  184.      encounters errors during the link process; it exits without
  185.      writing an output file when it issues any error whatsoever.
  186. `-o OUTPUT'
  187.      Use OUTPUT as the name for the program produced by `ld'; if this
  188.      option is not specified, the name `a.out' is used by default.  The
  189.      script command `OUTPUT' can also specify the output file name.
  190. `-R FILENAME'
  191.      On some platforms, this option performs global optimizations that
  192.      become possible when the linker resolves addressing in the
  193.      program, such as relaxing address modes and synthesizing new
  194.      instructions in the output object file.
  195. `-relax'
  196.      An option with machine dependent effects.  Currently this option
  197.      is only supported on the H8/300.
  198.      On some platforms, use option performs global optimizations that
  199.      become possible when the linker resolves addressing in the
  200.      program, such as relaxing address modes and synthesizing new
  201.      instructions in the output object file.
  202.      On platforms where this is not supported, `-relax' is accepted, but
  203.      ignored.
  204.      Generate relocatable output--i.e., generate an output file that
  205.      can in turn serve as input to `ld'.  This is often called "partial
  206.      linking".  As a side effect, in environments that support standard
  207.      Unix magic numbers, this option also sets the output file's magic
  208.      number to `OMAGIC'.  If this option is not specified, an absolute
  209.      file is produced.  When linking C++ programs, this option *will
  210.      not* resolve references to constructors; to do that, use `-Ur'.
  211.      This option does the same as `-i'.
  212.      Omit debugger symbol information (but not all symbols) from the
  213.      output file.
  214.      Omit all symbol information from the output file.
  215. `{ SCRIPT }'
  216.      You can, if you wish, include a script of linker commands directly
  217.      in the command line instead of referring to it via an input file.
  218.      When the character `{' occurs on the command line, the linker
  219.      switches to interpreting the command language until the end of the
  220.      list of commands is reached; the end is indicated with a closing
  221.      brace `}'.  `ld' does not recognize other command-line options
  222.      while parsing the script.  *Note Commands::, for a description of
  223.      the command language.
  224. `-Tbss BSSORG'
  225. `-Tdata DATAORG'
  226. `-Ttext TEXTORG'
  227.      Use ORG as the starting address for--respectively--the `bss',
  228.      `data', or the `text' segment of the output file.  ORG must be a
  229.      single hexadecimal integer; for compatibility with other linkers,
  230.      you may omit the leading `0x' usually associated with hexadecimal
  231.      values.
  232. `-T COMMANDFILE'
  233. `-TCOMMANDFILE'
  234.      Read link commands from the file COMMANDFILE.  These commands
  235.      completely override `ld''s default link format (rather than adding
  236.      to it); COMMANDFILE must specify everything necessary to describe
  237.      the target format.  *Note Commands::.
  238.      You may also include a script of link commands directly in the
  239.      command line by bracketing it between `{' and `}'.
  240.      Print the names of the input files as `ld' processes them.
  241. `-u SYM'
  242.      Force SYM to be entered in the output file as an undefined symbol.
  243.      Doing this may, for example, trigger linking of additional modules
  244.      from standard libraries.  `-u' may be repeated with different
  245.      option arguments to enter additional undefined symbols.
  246. `-Ur'
  247.      For anything other than C++ programs, this option is equivalent to
  248.      `-r': it generates relocatable output--i.e., an output file that
  249.      can in turn serve as input to `ld'.  When linking C++ programs,
  250.      `-Ur' *will* resolve references to constructors, unlike `-r'.
  251.      Display the version number for `ld'.
  252.      If `-s' or `-S' is also specified, delete only local symbols
  253.      beginning with `L'.
  254.      If `-s' or `-S' is also specified, delete all local symbols, not
  255.      just those beginning with `L'.
  256. `-ySYMBOL'
  257.      Print the name of each linked file in which SYMBOL appears.  This
  258.      option may be given any number of times.  On many systems it is
  259.      necessary to prepend an underscore.
  260.      This option is useful when you have an undefined symbol in your
  261.      link but don't know where the reference is coming from.
  262. File: ld.info,  Node: Commands,  Next: BFD,  Prev: Invocation,  Up: Top
  263. Command Language
  264. ****************
  265.    The command language provides explicit control over the link process,
  266. allowing complete specification of the mapping between the linker's
  267. input files and its output.  It controls:
  268.    * input files
  269.    * file formats
  270.    * output file format
  271.    * addresses of sections
  272.    * placement of common blocks
  273.    You may supply a command file (also known as a link script) to the
  274. linker either explicitly through the `-T' option, or implicitly as an
  275. ordinary file. If the linker opens a file which it cannot recognize as
  276. a supported object or archive format, it tries to interpret the file as
  277. a command file.
  278.    You can also include a script directly on the `ld' command line,
  279. delimited by the characters `{' and `}'.
  280. * Menu:
  281. * Scripts::                     Linker Scripts
  282. * Expressions::                 Expressions
  283. * MEMORY::                      MEMORY Command
  284. * SECTIONS::                    SECTIONS Command
  285. * Entry Point::                 The Entry Point
  286. * Other Commands::              Other Commands
  287. File: ld.info,  Node: Scripts,  Next: Expressions,  Up: Commands
  288. Linker Scripts
  289. ==============
  290.    The `ld' command language is a collection of statements; some are
  291. simple keywords setting a particular option, some are used to select and
  292. group input files or name output files; and two statement types have a
  293. fundamental and pervasive impact on the linking process.
  294.    The most fundamental command of the `ld' command language is the
  295. `SECTIONS' command (*note SECTIONS::.).  Every meaningful command
  296. script must have a `SECTIONS' command: it specifies a "picture" of the
  297. output file's layout, in varying degrees of detail.  No other command
  298. is required in all cases.
  299.    The `MEMORY' command complements `SECTIONS' by describing the
  300. available memory in the target architecture.  This command is optional;
  301. if you don't use a `MEMORY' command, `ld' assumes sufficient memory is
  302. available in a contiguous block for all output.  *Note MEMORY::.
  303.    You may include comments in linker scripts just as in C: delimited
  304. by `/*' and `*/'.  As in C, comments are syntactically equivalent to
  305. whitespace.
  306. File: ld.info,  Node: Expressions,  Next: MEMORY,  Prev: Scripts,  Up: Commands
  307. Expressions
  308. ===========
  309.    Many useful commands involve arithmetic expressions.  The syntax for
  310. expressions in the command language is identical to that of C
  311. expressions, with the following features:
  312.    * All expressions evaluated as integers and are of "long" or
  313.      "unsigned long" type.
  314.    * All constants are integers.
  315.    * All of the C arithmetic operators are provided.
  316.    * You may reference, define, and create global variables.
  317.    * You may call special purpose built-in functions.
  318. * Menu:
  319. * Integers::                    Integers
  320. * Symbols::                     Symbol Names
  321. * Location Counter::            The Location Counter
  322. * Operators::                   Operators
  323. * Evaluation::                  Evaluation
  324. * Assignment::                  Assignment: Defining Symbols
  325. * Built-ins::                   Built-In Functions
  326. File: ld.info,  Node: Integers,  Next: Symbols,  Up: Expressions
  327. Integers
  328. --------
  329.    An octal integer is `0' followed by zero or more of the octal digits
  330. (`01234567').
  331.      _as_octal = 0157255;
  332.    A decimal integer starts with a non-zero digit followed by zero or
  333. more digits (`0123456789').
  334.      _as_decimal = 57005;
  335.    A hexadecimal integer is `0x' or `0X' followed by one or more
  336. hexadecimal digits chosen from `0123456789abcdefABCDEF'.
  337.      _as_hex = 0xdead;
  338.    To write a negative integer, use the prefix operator `-'; *note
  339. Operators::..
  340.      _as_neg = -57005;
  341.    Additionally the suffixes `K' and `M' may be used to scale a
  342. constant by `1024' or `1024*1024' respectively. For example, the
  343. following all refer to the same quantity:
  344.              _fourk_1 = 4K;
  345.              _fourk_2 = 4096;
  346.              _fourk_3 = 0x1000;
  347. File: ld.info,  Node: Symbols,  Next: Location Counter,  Prev: Integers,  Up: Expressions
  348. Symbol Names
  349. ------------
  350.    Unless quoted, symbol names start with a letter, underscore, point or
  351. hyphen and may include any letters, underscores, digits, points, and
  352. minus signs.  Unquoted symbol names must not conflict with any
  353. keywords.  You can specify a symbol which contains odd characters or has
  354. the same name as a keyword, by surrounding the symbol name in double
  355. quotes:
  356.              "SECTION" = 9;
  357.              "with a space" = "also with a space" + 10;
  358. File: ld.info,  Node: Location Counter,  Next: Operators,  Prev: Symbols,  Up: Expressions
  359. The Location Counter
  360. --------------------
  361.    The special linker variable "dot" `.' always contains the current
  362. output location counter. Since the `.' always refers to a location in
  363. an output section, it must always appear in an expression within a
  364. `SECTIONS' command. The `.' symbol may appear anywhere that an ordinary
  365. symbol is allowed in an expression, but its assignments have a side
  366. effect. Assigning a value to the `.' symbol will cause the location
  367. counter to be moved.  This may be used to create holes in the output
  368. section.  The location counter may never be moved backwards.
  369.      SECTIONS
  370.      {
  371.              output :
  372.              {
  373.              file1(.text)
  374.              . = . + 1000;
  375.              file2(.text)
  376.              . += 1000;
  377.              file3(.text)
  378.              } = 0x1234;
  379.      }
  380. In the previous example, `file1' is located at the beginning of the
  381. output section, then there is a 1000 byte gap.  Then `file2' appears,
  382. also with a 1000 byte gap following before `file3' is loaded.  The
  383. notation `= 0x1234' specifies what data to write in the gaps (*note
  384. Section Options::.).
  385. File: ld.info,  Node: Operators,  Next: Evaluation,  Prev: Location Counter,  Up: Expressions
  386. Operators
  387. ---------
  388.    The linker recognizes  the standard C set of arithmetic operators,
  389. with the standard bindings and precedence levels:
  390.      precedence      associativity   Operators                Notes
  391.      (highest)
  392.      1               left            !  -  ~                  (1)
  393.      2               left            *  /  %
  394.      3               left            +  -
  395.      4               left            >>  <<
  396.      5               left            ==  !=  >  <  <=  >=
  397.      6               left            &
  398.      7               left            |
  399.      8               left            &&
  400.      9               left            ||
  401.      10              right           ? :
  402.      11              right           &=  +=  -=  *=  /=       (2)
  403.      (lowest)
  404.    Notes: (1) Prefix operators (2) *Note Assignment::
  405. File: ld.info,  Node: Evaluation,  Next: Assignment,  Prev: Operators,  Up: Expressions
  406. Evaluation
  407. ----------
  408.    The linker uses "lazy evaluation" for expressions; it only calculates
  409. an expression when absolutely necessary.  The linker needs the value of
  410. the start address, and the lengths of memory regions, in order to do any
  411. linking at all; these values are computed as soon as possible when the
  412. linker reads in the command file.  However, other values (such as symbol
  413. values) are not known or needed until after storage allocation.  Such
  414. values are evaluated later, when other information (such as the sizes of
  415. output sections) is available for use in the symbol assignment
  416. expression.
  417. File: ld.info,  Node: Assignment,  Next: Built-ins,  Prev: Evaluation,  Up: Expressions
  418. Assignment: Defining Symbols
  419. ----------------------------
  420.    You may create global symbols, and assign values (addresses) to
  421. global symbols, using any of the C assignment operators:
  422. `SYMBOL  = EXPRESSION ;'
  423. `SYMBOL &= EXPRESSION ;'
  424. `SYMBOL += EXPRESSION ;'
  425. `SYMBOL -= EXPRESSION ;'
  426. `SYMBOL *= EXPRESSION ;'
  427. `SYMBOL /= EXPRESSION ;'
  428.    Two things distinguish assignment from other operators in `ld'
  429. expressions.
  430.    * Assignment may only be used at the root of an expression; `a=b+3;'
  431.      is allowed, but `a+b=3;' is an error.
  432.    * You must place a trailing semicolon (";") at the end of an
  433.      assignment statement.
  434.    Assignment statements may appear:
  435.    * as commands in their own right in an `ld' script; or
  436.    * as independent statements within a `SECTIONS' command; or
  437.    * as part of the contents of a section definition in a `SECTIONS'
  438.      command.
  439.    The first two cases are equivalent in effect--both define a symbol
  440. with an absolute address.  The last case defines a symbol whose address
  441. is relative to a particular section (*note SECTIONS::.).
  442.    When a linker expression is evaluated and assigned to a variable, it
  443. is given either an absolute or a relocatable type. An absolute
  444. expression type is one in which the symbol contains the value that it
  445. will have in the output file, a relocatable expression type is one in
  446. which the value is expressed as a fixed offset from the base of a
  447. section.
  448.    The type of the expression is controlled by its position in the
  449. script file.  A symbol assigned within a section definition is created
  450. relative to the base of the section; a symbol assigned in any other
  451. place is created as an absolute symbol. Since a symbol created within a
  452. section definition is relative to the base of the section, it will
  453. remain relocatable if relocatable output is requested.  A symbol may be
  454. created with an absolute value even when assigned to within a section
  455. definition by using the absolute assignment function `ABSOLUTE'.  For
  456. example, to create an absolute symbol whose address is the last byte of
  457. an output section named `.data':
  458.      SECTIONS{ ...
  459.      .data :
  460.              {
  461.                      *(.data)
  462.                      _edata = ABSOLUTE(.) ;
  463.              }
  464.      ... }
  465.    The linker tries to put off the evaluation of an assignment until all
  466. the terms in the source expression are known (*note Evaluation::.).  For
  467. instance, the sizes of sections cannot be known until after allocation,
  468. so assignments dependent upon these are not performed until after
  469. allocation. Some expressions, such as those depending upon the location
  470. counter "dot", `.' must be evaluated during allocation. If the result
  471. of an expression is required, but the value is not available, then an
  472. error results.  For example, a script like the following
  473.      SECTIONS { ...
  474.            text 9+this_isnt_constant :
  475.                      { ...
  476.                      }
  477.      ... }
  478. will cause the error message "`Non constant expression for initial
  479. address'".
  480. File: ld.info,  Node: Built-ins,  Prev: Assignment,  Up: Expressions
  481. Built-In Functions
  482. ------------------
  483.    The command language includes a number of built-in functions for use
  484. in link script expressions.
  485. `ABSOLUTE(EXP)'
  486.      Return the absolute (non-relocatable, as opposed to non-negative)
  487.      value of the expression EXP.  Primarily useful to assign an
  488.      absolute value to a symbol within a section definition, where
  489.      symbol values are normally section-relative.
  490. `ADDR(SECTION)'
  491.      Return the absolute address of the named SECTION.  Your script must
  492.      previously have defined the location of that section. In the
  493.      following example, `symbol_1' and `symbol_2' are assigned identical
  494.      values:
  495.           SECTIONS{ ...
  496.                   .output1 :
  497.                           {
  498.                           start_of_output_1 = ABSOLUTE(.);
  499.                           ...
  500.                           }
  501.                   .output :
  502.                           {
  503.                           symbol_1 = ADDR(.output1);
  504.                           symbol_2 = start_of_output_1;
  505.                           }
  506.           ... }
  507. `ALIGN(EXP)'
  508.      Return the result of the current location counter (`.') aligned to
  509.      the next EXP boundary.  EXP must be an expression whose value is a
  510.      power of two.  This is equivalent to
  511.           (. + EXP - 1) & ~(EXP - 1)
  512.      `ALIGN' doesn't change the value of the location counter--it just
  513.      does arithmetic on it.  As an example, to align the output `.data'
  514.      section to the next `0x2000' byte boundary after the preceding
  515.      section and to set a variable within the section to the next
  516.      `0x8000' boundary after the input sections:
  517.           SECTIONS{ ...
  518.                   .data ALIGN(0x2000): {
  519.                           *(.data)
  520.                           variable = ALIGN(0x8000);
  521.                   }
  522.           ... }
  523.      The first use of `ALIGN' in this example specifies the location of
  524.      a section because it is used as the optional START attribute of a
  525.      section definition (*note Section Options::.).  The second use
  526.      simply defines the value of a variable.
  527.      The built-in `NEXT' is closely related to `ALIGN'.
  528. `DEFINED(SYMBOL)'
  529.      Return 1 if SYMBOL is in the linker global symbol table and is
  530.      defined, otherwise return 0.  You can use this function to provide
  531.      default values for symbols.  For example, the following
  532.      command-file fragment shows how to set a global symbol `begin' to
  533.      the first location in the `.text' section--but if a symbol called
  534.      `begin' already existed, its value is preserved:
  535.           SECTIONS{ ...
  536.                   .text : {
  537.                           begin = DEFINED(begin) ? begin : . ;
  538.                           ...
  539.                   }
  540.           ... }
  541. `NEXT(EXP)'
  542.      Return the next unallocated address that is a multiple of EXP.
  543.      This function is closely related to `ALIGN(EXP)'; unless you use
  544.      the `MEMORY' command to define discontinuous memory for the output
  545.      file, the two functions are equivalent.
  546. `SIZEOF(SECTION)'
  547.      Return the size in bytes of the named SECTION, if that section has
  548.      been allocated.  In the following example, `symbol_1' and
  549.      `symbol_2' are assigned identical values:
  550.           SECTIONS{ ...
  551.                   .output {
  552.                           .start = . ;
  553.                           ...
  554.                           .end = . ;
  555.                           }
  556.                   symbol_1 = .end - .start ;
  557.                   symbol_2 = SIZEOF(.output);
  558.           ... }
  559. `SIZEOF_HEADERS'
  560. `sizeof_headers'
  561.      Return the size in bytes of the output file's headers.  You can
  562.      use this number as the start address of the first section, if you
  563.      choose, to facilitate paging.
  564. File: ld.info,  Node: MEMORY,  Next: SECTIONS,  Prev: Expressions,  Up: Commands
  565. MEMORY Command
  566. ==============
  567.    The linker's default configuration permits allocation of all
  568. available memory.  You can override this configuration by using the
  569. `MEMORY' command.  The `MEMORY' command describes the location and size
  570. of blocks of memory in the target.  By using it carefully, you can
  571. describe which memory regions may be used by the linker, and which
  572. memory regions it must avoid.  The linker does not shuffle sections to
  573. fit into the available regions, but does move the requested sections
  574. into the correct regions and issue errors when the regions become too
  575. full.
  576.    The command files may contain at most one use of the `MEMORY'
  577. command; however, you can define as many blocks of memory within it as
  578. you wish.  The syntax is:
  579.      MEMORY
  580.            {
  581.             NAME (ATTR) : ORIGIN = ORIGIN, LENGTH = LEN
  582.             ...
  583.            }
  584. `NAME'
  585.      is a name used internally by the linker to refer to the region. Any
  586.      symbol name may be used.  The region names are stored in a separate
  587.      name space, and will not conflict with symbols, file names or
  588.      section names.  Use distinct names to specify multiple regions.
  589. `(ATTR)'
  590.      is an optional list of attributes, permitted for compatibility
  591.      with the AT&T linker but not used by `ld' beyond checking that the
  592.      attribute list is valid.  Valid attribute lists must be made up of
  593.      the characters "`LIRWX'".  If you omit the attribute list, you may
  594.      omit the parentheses around it as well.
  595. `ORIGIN'
  596.      is the start address of the region in physical memory.  It is an
  597.      expression that must evaluate to a constant before memory
  598.      allocation is performed. The keyword `ORIGIN' may be abbreviated
  599.      to `org' or `o'.
  600. `LEN'
  601.      is the size in bytes of the region (an expression).  The keyword
  602.      `LENGTH' may be abbreviated to `len' or `l'.
  603.    For example, to specify that memory has two regions available for
  604. allocation--one starting at 0 for 256 kilobytes, and the other starting
  605. at `0x40000000' for four megabytes:
  606.      MEMORY
  607.              {
  608.              rom : ORIGIN = 0, LENGTH = 256K
  609.              ram : org = 0x40000000, l = 4M
  610.              }
  611.    Once you have defined a region of memory named MEM, you can direct
  612. specific output sections there by using a command ending in `>MEM'
  613. within the `SECTIONS' command (*note Section Options::.).  If the
  614. combined output sections directed to a region are too big for the
  615. region, the linker will issue an error message.
  616. File: ld.info,  Node: SECTIONS,  Next: Entry Point,  Prev: MEMORY,  Up: Commands
  617. SECTIONS Command
  618. ================
  619.    The `SECTIONS' command controls exactly where input sections are
  620. placed into output sections, their order and to which output sections
  621. they are allocated.
  622.    You may use at most one `SECTIONS' command in a commands file, but
  623. you can have as many statements within it as you wish.  Statements
  624. within the `SECTIONS' command can do one of three things:
  625.    * define the entry point;
  626.    * assign a value to a symbol;
  627.    * describe the placement of a named output section, and what input
  628.      sections make it up.
  629.    The first two possibilities--defining the entry point, and defining
  630. symbols--can also be done outside the `SECTIONS' command: *note Entry
  631. Point::., *note Assignment::..  They are permitted here as well for
  632. your convenience in reading the script, so that symbols or the entry
  633. point can be defined at meaningful points in your output-file layout.
  634.    When no `SECTIONS' command is specified, the default action of the
  635. linker is to place each input section into an identically named output
  636. section in the order that the sections are first encountered in the
  637. input files; if all input sections are present in the first file, for
  638. example, the order of sections in the output file will match the order
  639. in the first input file.
  640. * Menu:
  641. * Section Definition::          Section Definitions
  642. * Section Contents::            Section Contents
  643. * Section Options::             Optional Section Attributes
  644. File: ld.info,  Node: Section Definition,  Next: Section Contents,  Up: SECTIONS
  645. Section Definitions
  646. -------------------
  647.    The most frequently used statement in the `SECTIONS' command is the
  648. "section definition", which you can use to specify the properties of an
  649. output section: its location, alignment, contents, fill pattern, and
  650. target memory region.  Most of these specifications are optional; the
  651. simplest form of a section definition is
  652.      SECTIONS { ...
  653.      SECNAME : {
  654.                       CONTENTS
  655.                      }
  656.      ... }
  657. SECNAME is the name of the output section, and CONTENTS a specification
  658. of what goes there--for example, a list of input files or sections of
  659. input files.  As you might assume, the whitespace shown is optional.
  660. You do need the colon `:' and the braces `{}', however.
  661.    SECNAME must meet the constraints of your output format.  In formats
  662. which only support a limited number of sections, such as `a.out', the
  663. name must be one of the names supported by the format (`a.out', for
  664. example, allows only `.text', `.data' or `.bss'). If the output format
  665. supports any number of sections, but with numbers and not names (as is
  666. the case for Oasys), the name should be supplied as a quoted numeric
  667. string.  A section name may consist of any sequence characters, but any
  668. name which does not conform to the standard `ld' symbol name syntax
  669. must be quoted.  *Note Symbol Names: Symbols.
  670. File: ld.info,  Node: Section Contents,  Next: Section Options,  Prev: Section Definition,  Up: SECTIONS
  671. Section Contents
  672. ----------------
  673.    In a section definition, you can specify the contents of an output
  674. section by listing particular object files, by listing particular
  675. input-file sections, or by a combination of the two.  You can also
  676. place arbitrary data in the section, and define symbols relative to the
  677. beginning of the section.
  678.    The CONTENTS of a section definition may include any of the
  679. following kinds of statement.  You can include as many of these as you
  680. like in a single section definition, separated from one another by
  681. whitespace.
  682. `FILENAME'
  683.      You may simply name a particular input file to be placed in the
  684.      current output section; *all* sections from that file are placed
  685.      in the current section definition. To specify a list of particular
  686.      files by name:
  687.           .data : { afile.o bfile.o cfile.o }
  688.      The example also illustrates that multiple statements can be
  689.      included in the contents of a section definition, since each file
  690.      name is a separate statement.
  691.      If the file name has already been mentioned in another section
  692.      definition, with an explicit section name list, then only those
  693.      sections which have not yet been allocated are used.
  694. `FILENAME( SECTION )'
  695. `FILENAME( SECTION, SECTION, ... )'
  696. `FILENAME( SECTION SECTION ... )'
  697.      You can name one or more sections from your input files, for
  698.      insertion in the current output section.  If you wish to specify a
  699.      list of input-file sections inside the parentheses, you may
  700.      separate the section names by either commas or whitespace.
  701. `* (SECTION)'
  702. `* (SECTION, SECTION, ...)'
  703. `* (SECTION SECTION ...'
  704.      Instead of explicitly naming particular input files in a link
  705.      control script, you can refer to *all* files from the `ld' command
  706.      line: use `*' instead of a particular file name before the
  707.      parenthesized input-file section list.
  708.      For example, to copy sections `1' through `4' from an Oasys file
  709.      into the `.text' section of an `a.out' file, and sections `13' and
  710.      `14' into the `.data' section:
  711.           SECTIONS {
  712.                   .text :{
  713.                           *("1" "2" "3" "4")
  714.                   }
  715.           
  716.                   .data :{
  717.                           *("13" "14")
  718.                   }
  719.           }
  720.      If you have already explicitly included some files by name, `*'
  721.      refers to all *remaining* files--those whose places in the output
  722.      file have not yet been defined.
  723. `[ SECTION ]'
  724. `[ SECTION, SECTION, ... ]'
  725. `[ SECTION SECTION ... ]'
  726.      This is an alternate notation to specify named sections from all
  727.      unallocated input files; its effect is exactly the same as that of
  728.      `* (SECTION...)'
  729. `FILENAME`( COMMON )''
  730. `( COMMON )'
  731.      Specify where in your output file to place uninitialized data with
  732.      this notation.  `*(COMMON)' by itself refers to all uninitialized
  733.      data from all input files (so far as it is not yet allocated);
  734.      FILENAME`(COMMON)' refers to uninitialized data from a particular
  735.      file.  Both are special cases of the general mechanisms for
  736.      specifying where to place input-file sections: `ld' permits you to
  737.      refer to uninitialized data as if it were in an input-file section
  738.      named `COMMON', regardless of the input file's format.
  739.    For example, the following command script arranges the output file
  740. into three consecutive sections, named `.text', `.data', and `.bss',
  741. taking the input for each from the correspondingly named sections of
  742. all the input files:
  743.      SECTIONS {
  744.        .text : { *(.text) }
  745.        .data : { *(.data) }
  746.        .bss :  { *(.bss)  *(COMMON) }
  747.      }
  748.    The following example reads all of the sections from file `all.o'
  749. and places them at the start of output section `outputa' which starts
  750. at location `0x10000'. All of section `.input1' from file `foo.o'
  751. follows immediately, in the same output section.  All of section
  752. `.input2' from `foo.o' goes into output section `outputb', followed by
  753. section `.input1' from `foo1.o'.  All of the remaining `.input1' and
  754. `.input2' sections from any files are written to output section
  755. `outputc'.
  756.      SECTIONS {
  757.              outputa 0x10000 :
  758.                      {
  759.                      all.o
  760.                      foo.o (.input1)
  761.                      }
  762.              outputb :
  763.                      {
  764.                      foo.o (.input2)
  765.                      foo1.o (.input1)
  766.                      }
  767.              outputc :
  768.                      {
  769.                      *(.input1)
  770.                      *(.input2)
  771.                      }
  772.      }
  773.    There are still more kinds of statements permitted in the contents of
  774. output section definitions.  The foregoing statements permitted you to
  775. arrange, in your output file, data originating from your input files.
  776. You can also place data directly in an output section from the link
  777. command script.  Most of these additional statements involve
  778. expressions; *note Expressions::..  Although these statements are shown
  779. separately here for ease of presentation, no such segregation is needed
  780. within a section definition in the `SECTIONS' command; you can intermix
  781. them freely with any of the statements we've just described.
  782. `CREATE_OBJECT_SYMBOLS'
  783.      Create a symbol for each input file in the current section, set to
  784.      the address of the first byte of data written from the input file.
  785.      For instance, with `a.out' files it is conventional to have a
  786.      symbol for each input file.  You can accomplish this by defining
  787.      the output `.text' section as follows:
  788.           SECTIONS {
  789.                   .text 0x2020 :
  790.                            {
  791.                           CREATE_OBJECT_SYMBOLS
  792.                           *(.text)
  793.                           _etext = ALIGN(0x2000);
  794.                           }
  795.                   ...
  796.           }
  797.      If `objsym' is a file containing this script, and `a.o', `b.o',
  798.      `c.o', and `d.o' are four input files with contents like the
  799.      following--
  800.           /* a.c */
  801.           
  802.           afunction() { }
  803.           int adata=1;
  804.           int abss;
  805.      `ld -M sample a.o b.o c.o d.o' would create a map like this,
  806.      containing symbols matching the object file names:
  807.           00000000 A __DYNAMIC
  808.           00004020 B _abss
  809.           00004000 D _adata
  810.           00002020 T _afunction
  811.           00004024 B _bbss
  812.           00004008 D _bdata
  813.           00002038 T _bfunction
  814.           00004028 B _cbss
  815.           00004010 D _cdata
  816.           00002050 T _cfunction
  817.           0000402c B _dbss
  818.           00004018 D _ddata
  819.           00002068 T _dfunction
  820.           00004020 D _edata
  821.           00004030 B _end
  822.           00004000 T _etext
  823.           00002020 t a.o
  824.           00002038 t b.o
  825.           00002050 t c.o
  826.           00002068 t d.o
  827. `SYMBOL = EXPRESSION ;'
  828. `SYMBOL F= EXPRESSION ;'
  829.      SYMBOL is any symbol name (*note Symbols::.).  "F=" refers to any
  830.      of the operators `&= += -= *= /=' which combine arithmetic and
  831.      assignment.
  832.      When you assign a value to a symbol within a particular section
  833.      definition, the value is relative to the beginning of the section
  834.      (*note Assignment::.).  If you write
  835.           SECTIONS {
  836.             abs = 14 ;
  837.             ...
  838.             .data : { ... rel = 14 ; ... }
  839.             abs2 = 14 + ADDR(.data);
  840.             ...
  841.           }
  842.      `abs' and `rel' do not have the same value; `rel' has the same
  843.      value as `abs2'.
  844. `BYTE(EXPRESSION)'
  845. `SHORT(EXPRESSION)'
  846. `LONG(EXPRESSION)'
  847.      By including one of these three statements in a section
  848.      definition, you can explicitly place one, two, or four bytes
  849.      (respectively) at the current address of that section.
  850.      Multiple-byte quantities are represented in whatever byte order is
  851.      appropriate for the output file format (*note BFD::.).
  852. `FILL(EXPRESSION)'
  853.      Specifies the "fill pattern" for the current section.  Any
  854.      otherwise unspecified regions of memory within the section (for
  855.      example, regions you skip over by assigning a new value to the
  856.      location counter `.') are filled with the two least significant
  857.      bytes from the EXPRESSION argument.  A `FILL' statement covers
  858.      memory locations *after* the point it occurs in the section
  859.      definition; by including more than one `FILL' statement, you can
  860.      have different fill patterns in different parts of an output
  861.      section.
  862. File: ld.info,  Node: Section Options,  Prev: Section Contents,  Up: SECTIONS
  863. Optional Section Attributes
  864. ---------------------------
  865.    Here is the full syntax of a section definition, including all the
  866. optional portions:
  867.      SECTIONS {
  868.      ...
  869.      SECNAME START BLOCK(ALIGN) (NOLOAD) : { CONTENTS } =FILL >REGION
  870.      ...
  871.      }
  872.    SECNAME and CONTENTS are required.  *Note Section Definition::, and
  873. *note Section Contents::. for details on CONTENTS.  The remaining
  874. elements--START, `BLOCK(ALIGN)', `(NOLOAD)' `=FILL', and `>REGION'--are
  875. all optional.
  876. `START'
  877.      You can force the output section to be loaded at a specified
  878.      address by specifying START immediately following the section name.
  879.      sTART can be represented as any expression. The following example
  880.      generates section OUTPUT at location `0x40000000':
  881.           SECTIONS {
  882.                   ...
  883.                   output 0x40000000: {
  884.                          ...
  885.                     }
  886.                   ...
  887.           }
  888. `BLOCK(ALIGN)'
  889.      You can include `BLOCK()' specification to advance the location
  890.      counter `.' prior to the beginning of the section, so that the
  891.      section will begin at the specified alignment.  ALIGN is an
  892.      expression.
  893. `(NOLOAD)'
  894.      Use `(NOLOAD)' to prevent a section from being loaded into memory
  895.      each time it is accessed.  For example, in the script sample
  896.      below, the `ROM' segment is addressed at memory location `0' and
  897.      does not need to be loaded into each object file:
  898.           SECTIONS {
  899.                   ROM  0  (NOLOAD)  : { ... }
  900.                   ...
  901.           }
  902. `=FILL'
  903.      Including `=FILL' in a section definition specifies the initial
  904.      fill value for that section.  You may use any expression to
  905.      specify FILL.  Any unallocated holes in the current output section
  906.      when written to the output file will be filled with the two least
  907.      significant bytes of the value, repeated as necessary.  You can
  908.      also change the fill value with a `FILL' statement in the CONTENTS
  909.      of a section definition.
  910. `>REGION'
  911.      Assign this section to a previously defined region of memory.
  912.      *Note MEMORY::.
  913. File: ld.info,  Node: Entry Point,  Next: Other Commands,  Prev: SECTIONS,  Up: Commands
  914. The Entry Point
  915. ===============
  916.    The linker command language includes a command specifically for
  917. defining the first executable instruction in an output file (its "entry
  918. point").  Its argument is a symbol name:
  919.      ENTRY(SYMBOL)
  920.    Like symbol assignments, the `ENTRY' command may be placed either as
  921. an independent command in the command file, or among the section
  922. definitions within the `SECTIONS' command--whatever makes the most
  923. sense for your layout.
  924.    `ENTRY' is only one of several ways of choosing the entry point.
  925. You may indicate it in any of the following ways (shown in descending
  926. order of priority: methods higher in the list override methods lower
  927. down).
  928.    * the `-e' ENTRY command-line option;
  929.    * the `ENTRY(SYMBOL' command in a linker control script;
  930.    * the value of the symbol `start', if present;
  931.    * the value of the symbol `_main', if present;
  932.    * the address of the first byte of the `.text' section, if present;
  933.    * The address `0'.
  934.    For example, you can use these rules to generate an entry point with
  935. an assignment statement: if no symbol `start' is defined within your
  936. input files, you can simply define it, assigning it an appropriate
  937. value--
  938.      start = 0x2020;
  939. The example shows an absolute address, but you can use any expression.
  940. For example, if your input object files use some other symbol-name
  941. convention for the entry point, you can just assign the value of
  942. whatever symbol contains the start address to `start':
  943.      start = other_symbol ;
  944. File: ld.info,  Node: Other Commands,  Prev: Entry Point,  Up: Commands
  945. Other Commands
  946. ==============
  947.    The command language includes a number of other commands that you can
  948. use for specialized purposes.  They are similar in purpose to
  949. command-line options.
  950. `FLOAT'
  951. `NOFLOAT'
  952.      These keywords were used in some older linkers to request a
  953.      particular math subroutine library.  `ld' doesn't use the
  954.      keywords, assuming instead that any necessary subroutines are in
  955.      libraries specified using the general mechanisms for linking to
  956.      archives; but to permit the use of scripts that were written for
  957.      the older linkers, the keywords `FLOAT' and `NOFLOAT' are accepted
  958.      and ignored.
  959. `FORCE_COMMON_ALLOCATION'
  960.      This command has the same effect as the `-d' command-line option:
  961.      to make `ld' assign space to common symbols even if a relocatable
  962.      output file is specified (`-r').
  963. `INPUT ( FILE, FILE, ... )'
  964. `INPUT ( FILE FILE ... )'
  965.      Use this command to include binary input files in the link, without
  966.      including them in a particular section definition.  Files
  967.      specified this way are treated identically to object files listed
  968.      on the command line.
  969. `OUTPUT ( FILENAME )'
  970.      Use this command to name the link output file FILENAME.  The
  971.      effect of `OUTPUT(FILENAME)' is identical to the effect of
  972.      `-o FILENAME', and whichever is encountered last will control the
  973.      name actually used to name the output file.  In particular, you
  974.      can use this command to supply a default output-file name other
  975.      than `a.out'.
  976. `OUTPUT_ARCH ( BFDNAME )'
  977.      Specify a particular output machine architecture, with one of the
  978.      names used by the BFD back-end routines (*note BFD::.).  This
  979.      command is often unnecessary; the architecture is most often set
  980.      implicitly by either the system BFD configuration or as a side
  981.      effect of the `OUTPUT_FORMAT' command.
  982. `OUTPUT_FORMAT ( BFDNAME )'
  983.      Specify a particular output format, with one of the names used by
  984.      the BFD back-end routines (*note BFD::.).  This selection will
  985.      only affect the output file; the related command `TARGET' affects
  986.      primarily input files.
  987. `SEARCH_DIR ( PATH )'
  988.      Add PATH to the list of paths where `ld' looks for archive
  989.      libraries.  `SEARCH_DIR(PATH)' has the same effect as `-LPATH' on
  990.      the command line.
  991. `STARTUP ( FILENAME )'
  992.      Ensure that FILENAME is the first input file used in the link
  993.      process.
  994. `TARGET ( FORMAT )'
  995.      Change the input-file object code format (like the command-line
  996.      option `-b' or its synonym `-format').  The argument FORMAT is one
  997.      of the strings used by BFD to name binary formats.  In the current
  998.      `ld' implementation, if `TARGET' is specified but `OUTPUT_FORMAT'
  999.      is not, the last `TARGET' argument is also used as the default
  1000.      format for the `ld' output file.  *Note BFD::.
  1001.      If you don't use the `TARGET' command, `ld' uses the value of the
  1002.      environment variable `GNUTARGET', if available, to select the
  1003.      output file format.  If that variable is also absent, `ld' uses
  1004.      the default format configured for your machine in the BFD
  1005.      libraries.
  1006.